package xyz.zhouzhaodong.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;
import xyz.zhouzhaodong.crm.dto.DepartmentDto;
import xyz.zhouzhaodong.crm.entity.Department;
import xyz.zhouzhaodong.crm.entity.Department;
import xyz.zhouzhaodong.crm.mapper.DepartmentMapper;
import xyz.zhouzhaodong.crm.mapper.DepartmentMapper;
import xyz.zhouzhaodong.crm.service.IDepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import xyz.zhouzhaodong.crm.util.ExceptionEnum;
import xyz.zhouzhaodong.crm.util.RestMessage;

import javax.annotation.Resource;
import java.beans.Transient;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 部门表 服务实现类
 * </p>
 *
 * @author zhouzhaodong
 * @since 2020-06-10
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {

    @Resource
    DepartmentMapper departmentMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage addDepartment(DepartmentDto departmentDto) {
        try {
            RestMessage restMessage = selectDepartment(departmentDto);
            if (restMessage.getCode() != 0) {
                return restMessage;
            }
            // 随机生成id
            String id = UUID.randomUUID().toString().replaceAll("-", "");
            Department department = new Department();
            department.setId(id);
            department.setName(departmentDto.getName());
            // 创建人为登录人
            department.setCreateUser(departmentDto.getStaffId());
            department.setCreateTime(new Date());
            departmentMapper.insert(department);
            return RestMessage.success(id);
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage deleteDepartment(String id, String staffId) {
        try {
            Department department = new Department();
            department.setId(id);
            department.setFlag(1);
            department.setUpdateUser(staffId);
            department.setUpdateTime(new Date());
            departmentMapper.updateById(department);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage updateDepartment(DepartmentDto departmentDto) {
        try {
            RestMessage restMessage = selectDepartment(departmentDto);
            if (restMessage.getCode() != 0) {
                return restMessage;
            }
            departmentDto.setUpdateUser(departmentDto.getStaffId());
            departmentDto.setUpdateTime(new Date());
            departmentMapper.updateDepartment(departmentDto);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    public RestMessage selectOne(String id) {
        Department department = departmentMapper.selectById(id);
        return RestMessage.success(department);
    }

    @Override
    public RestMessage selectList(DepartmentDto departmentDto) {
        if(departmentDto.getFlag() == null){
            departmentDto.setFlag(0);
        }
        if (departmentDto.getPageNo() == 0 && departmentDto.getPageSize() == 0) {
            // 返回不分页数据
            List<Department> list = departmentMapper.selectDepartmentList(departmentDto);
            return RestMessage.success(list);
        } else {
            Page<Department> page = new Page<>(departmentDto.getPageNo(),
                    departmentDto.getPageSize() == 0 ? 10 : departmentDto.getPageSize());
            IPage<Department> list = departmentMapper.selectDepartmentList(departmentDto, page);
            return RestMessage.success(list);
        }
    }

    public RestMessage selectDepartment(DepartmentDto departmentDto) {
        // 首先需要查询是否存在该部门
        QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", departmentDto.getName());
        List<Department> list = departmentMapper.selectList(queryWrapper);
        if (list.size() > 0 && list.get(0) != null) {
            return new RestMessage(ExceptionEnum.DEPARTMENT_NAME.getCode(), ExceptionEnum.DEPARTMENT_NAME.getMsg());
        }
        return RestMessage.success();
    }

}
